
import io.vertx.core.Vertx
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.client.WebClient
import io.vertx.kotlin.core.json.jsonArrayOf
import org.jsoup.Jsoup
import java.io.FileWriter
import java.io.PrintWriter
import java.nio.charset.Charset


object Main{
  @JvmStatic
  fun main(args: Array<String>) {
    val client = WebClient.create(Vertx.vertx())
    var map: MutableMap<String, String> = mutableMapOf()
    client.getAbs("https://www.sn180.com/company/sellbuycate.html")
      .timeout(6000)
      .send()
      .onSuccess { res ->
        val body = res.bodyAsString()
        val document = Jsoup.parse(body)
        val elements = document.getElementsByClass("cats_b1")
        var js = jsonArrayOf()
        val path = "E:\\data\\category\\category.json"
        elements.forEach { element ->
          val category = JsonObject()
            map.put(element.text(),element.attr("href"))
          js.add(category.put("category",element.text()) )
        }
        PrintWriter(FileWriter(path, Charset.defaultCharset())).use { it.write(js.toString()) }
      }.onFailure { err -> println(err.message) }
    Thread.sleep(3000)
    map?.forEach { (k,s)-> client.getAbs(s).timeout(3000).send().onSuccess{
      res->
      println(k)
      val document = Jsoup.parse(res.bodyAsString())
      val elements = document.getElementsByClass("bb3")
      val path ="E:\\data\\json\\"+k+".json"
      var js = jsonArrayOf()
      elements.forEach { element ->
        var json =JsonObject()
        json.put("name",element.attr("title"))
        json.put("price",element.parent().nextElementSibling().text())
        js.add(json)
        println(json.toString())
      }
      PrintWriter(FileWriter(path, Charset.defaultCharset())).use { it.write(js.toString()) }
      println("=============================")
    } }
  }
  }




